<html>
<head>
  <script src="../backoff-xhr.js"></script>
  <style>
    body { background-color: black; }
    .test-title { padding-top: 20px; font-family: monospace; font-size: 20px; color: white; border-bottom: 1px solid lightgray;}
    .test-body { padding: 10px; border-left: 2px solid blue; }
    .assert-true { font-family: monospace; font-size: 16px; color: green; }
    .assert-false { font-family: monospace; font-size: 16px; color: red; }
  </style>
  <script>
  // Utility functions.
  String.prototype.escapeHTML = function() {
    return this.replace(/&/g,'&amp;').replace(/>/g,'&gt;').replace(/</g,'&lt;')
        .replace(/"/g,'&quot;');
  }
  function addhtml(html) {
    document.getElementById("main").innerHTML += html;
  }

  // Unittests
  // Simple framework to test.
  var tests = [];
  var currentTestHtml = "";
  function testend(i) {
    if (i > 0 && tests[i-1] !== null) throw "Error: previous test didn't succeed.";
    tests[i] = null;
    addhtml(currentTestHtml + "</div></div>");
  }
  function test(name, f) { tests.push({'name': name, 'f': f}); }

  function nexttest(i) {
    if (i >= tests.length) return;
    var t = tests[i];
    var output = "<div class='test'><div class='test-title'>" + t.name +
        "</div><div class='test-body'>";

    function assert(value) {
      var args = Array.prototype.slice.call(arguments, 1);
      if (value) {
        console.log.apply(console, args);
        output += "<div class='assert-true'>SUCCESS: " + args.join().escapeHTML() + "</div>";
      }
      else {
        console.error.apply(console, args);
        output += "<div class='assert-false'>FAILURE: " + args.join().escapeHTML() + "</div>";
      }
    }
    var end_called = false;

    try {
      t.f(assert, function end_test() {
          output += "</div></div>";
          end_called = true;
          addhtml(output);
          nexttest(i+1);
        });
    } catch(e) {
      console.warn(e.stack);
      assert(false, "Exception thrown: ", e);
      end_called = true;
      output += "</div></div>";
      addhtml(output);
      nexttest(i+1);
    }
  }
  function runtests() { nexttest(0); }

  // Simply open a URL to google.com and verify it works.
  test("base", function(assert, end) {
    var request = new BackoffXMLHttpRequest();
    var done = false;
    request.open("GET", "" + window.location, false);
    request.onreadystatechange = function() {
      assert(request.readyState == 4, "readyState is DONE");
      assert(request.status == 200, "Status 200 OK");
      done = true;
    }
    request.send();
    assert(done, "Request was performed correctly.");
    end();
  });

  // Verify that the content generated by the XMLHttpRequest is correct.
  test("content", function(assert, end) {
    var request = new BackoffXMLHttpRequest();
    request.open("GET", "golden.txt", false);
    request.onreadystatechange = function() {
      assert(request.responseText === "1234", "Response text is correct: \"" +
          request.responseText + "\"");
    }
    request.send();
    end();
  });

  // Same as above, but asynchronous.
  test("async", function(assert, end) {
    var request = new BackoffXMLHttpRequest();
    request.open("GET", "golden.txt", true);
    request.onreadystatechange = function() {
      if (request.readyState == 4) {
        assert(request.responseText === "1234", "Response text is correct: \"" +
            request.responseText + "\"");
        console.log(1);
        end();
      }
    }
    request.send();
  });

  // Cross domain should never work.
  test("cross-domain", function(assert, end) {
    var request = new BackoffXMLHttpRequest();
    var success = false;
    request.open("GET", "http://google.com", false);
    request.onreadystatechange = function() {
      // Shouldn't come here.
      success = false;
    }
    try {
      request.send();
      success = false;
    } catch(e) {
      success = true;
    }
    assert(success, "Transmission failed correctly.")
    end();
  });

  document.onreadystatechange = function(state) {
    if (document.readyState == "complete")
      runtests();
  }
  </script>
</head>
<body>
  <div id="main"></div>
</body>
</html>